import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/home/searchpopup',
        name: 'SearchPopup',
        component: () => import(/* webpackChunkName: "searchpopup" */ '../views/SearchPopup.vue'),
        meta: {
          isShowTabbar: false
        }
      }
    ],
    meta: {
      tabberActiveNum: 0,
      isShowTabbar: true
    }
  },
  {
    path: '/topic',
    name: 'Topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
    meta: {
      tabberActiveNum: 1,
      isShowTabbar: true
    }
  },

  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
    meta: {
      tabberActiveNum: 2,
      isShowTabbar: true
    }
  },

  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue'),
    meta: {
      tabberActiveNum: 3,
      isShowTabbar: true
    }
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    meta: {
      tabberActiveNum: 4,
      isShowTabbar: true
    }
  },
  {
    path: '/productdetail',
    name: 'ProductDetail',
    component: () => import(/* webpackChunkName: "productdetail" */ '../views/ProductDetail.vue'),
    meta: {
      isShowTabbar: false
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// // 全局路由守卫/ 路由拦截    能够在每一次跳转路由之前拦截下来,做一定的逻辑处理
// router.beforeEach((to, from, next) => {
//   // console.log("to是", to); // 要去往的路由对象
//   // console.log("from", from); // 从哪里出发的这个路由对象

//   // 判断是不是要进入购物车
//   if (to.path == "/cart") {
//     // console.log("进入到购物车");
//     // 判断有没有登录
//     // 如果没有登录,跳转到 /user 页面
//     let token = localStorage.getItem("token");
//     if (token) {
//       next()
//     } else {
//       // 拿不到token,说明没有登录,跳转到"我的"  /user
//       Vue.prototype.$toast.loading({
//         message: '请先登录...',
//         forbidClick: true,
//         duration: 800
//       });
//       setTimeout(() => {
//         next("/user")
//       }, 1000);
//     }
//     return
//   }
//   next(); // 必须调用next,才能跳转到想要去的页面.没有调用next,就无法跳转页面
// })

export default router
